<?php

namespace App\Http\Controllers\Admins;

use App\Http\Controllers\AdminController;
use App\Http\Controllers\Controller;
use App\Models\Auction;
use App\Models\ProductAuction;
use App\Models\SystemSeting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class Product extends AdminController
{
    public function list(Request $request)
    {
        $elem = \App\Models\Product::latest();

        $data = $elem->paginate($request->input('limit',10));

        return $this->ReturnPage($data);
    }

    public function listPost(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'id'    => 'required|integer',
            'name'  => 'required',
            'preview'   => 'required',
            'images'=> 'required|array',
            'price' => 'required|numeric',
            'user_id'   => 'required|integer',
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        \App\Models\Product::updateOrCreate(['id'=>$post['id']],$post);

        return $this->ReturnSuccessInfo();
    }

    public function listSet(Request $request)
    {
        $set = SystemSeting::firstOrCreate(['name'=>'product'],[
            'data'  => [
                'increase'  => 0,
                'integral'  => 0
            ]
        ]);

        return $this->ReturnSuccessInfo($set->data);
    }

    public function listSetPost(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'increase'  => 'required|integer',
            'integral'  => 'required|integer',
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        SystemSeting::updateOrCreate([
            'name'  => 'product'
        ],['data'=>$validator->validated()]);

        return $this->ReturnSuccessInfo();
    }

    public function listDelete(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'id'    => 'required|integer|exists:product,id'
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        \App\Models\Product::destroy($post['id']);

        return $this->ReturnSuccessInfo();
    }

    public function auction(Request $request)
    {
        $elem = ProductAuction::latest();

        $data = $elem->paginate($request->input('limit',10));

        return $this->ReturnPage($data);
    }

    public function auctionPost(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'start'     => 'required',
            'continue'  => 'required|integer',
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        ProductAuction::create($validator->validated());

        return $this->ReturnSuccessInfo();
    }

    public function auctionStop(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'id'    => 'required|integer'
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        $auction = ProductAuction::find($post['id']);
        $auction->status = 2;
        $auction->save();

        return $this->ReturnSuccessInfo();
    }

    public function auctionStart(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'id'    => 'required|integer'
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        $auction = ProductAuction::find($post['id']);
        $auction->start = date('H:i:s');
        $auction->status = 1;
        $auction->save();

        return $this->ReturnSuccessInfo();
    }

    public function auctionAuto(Request $request)
    {
        $data = SystemSeting::firstOrCreate(['name'=>'product_auto'],[
            'data'  => [
                'time'      => [],
                'continue'  => 0,
            ],
        ]);

        return $this->ReturnSuccessInfo([
            'time'      => implode('|',$data->data['time']),
            'continue'  => $data->data['continue'],
        ]);
    }

    public function auctionAutoSave(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'time'      => 'required',
            'continue'  => 'required|integer'
        ]);

        SystemSeting::updateOrCreate(['name'=>'product_auto'],[
            'data'  => [
                'time'      => explode('|',$post['time']),
                'continue'  => $post['continue']
            ],
        ]);

        return $this->ReturnSuccessInfo();
    }
}
